<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<%@ include file="/common/common-css.jsp" %>
	<link rel="stylesheet" href="${ctx}/ace/css/just-tip.css"/>
 <script>
	  cmdList = "/apiactivity/querylist.action";
 </script>
 <style>
	#jquery-table td{cursor:pointer !important}
	.load_bg{background:#000;opacity:0.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;}
	.load_img{left:50%;top:180px;position:fixed;height:50px;overflow:hidden;z-index:20000;}
 </style>
</head>
<body  class="no-skin">

<div  class="load_bg" style="display:none"></div>
<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>
	<div class="page-content">
		<form id="queryForm" action="${cmdList}">
			<div class="page-content-area">
				<div class="row">
					 <div class="col-sm-12">
						<div class="widget-box" style="border:0px solid #ccc; border-top:none;border-bottom:none;margin-top:15px;">
						<hr style="margin-top:0px;margin-bottom:0x">
						<div class="widget-header widget-header-blue widget-header-flat">
								<h4 class="widget-title">API活动管理</h4>
								<div class="widget-toolbar">
									<a href="#" data-action="collapse">
										<i class="ace-icon fa fa-chevron-up"></i>
									</a>
								</div>	
							</div>
							<div class=" widget-body">
								<div class="widget-main" >
								<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
									<label class="col-sm-6 control-label" style="text-align:right" for="name">名称: </label>
									<div class="col-sm-6">
									    <input class="form-control" type="text" name="name" id="name"/>
									</div>
								</div>
								
								<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
									<label class="col-sm-6 control-label" style="text-align:right" for="prd_name">状态: </label>
									<div class="col-sm-6">
									     <select name="status">
										   <option  value="">全部</option>
										   <option  value="0">下线</option>
										   <option  value="1">上线</option>
										  </select>
									</div>
								</div>
							</div><!-- /.widget-main -->
						  </div><!-- /.widget-body -->
						</div><!-- /.widget-box -->
					</div><!-- /.col-sm-12 -->
					
					<div class="col-md-12 center">
						<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3" style="text-align:right">
							<button class="btn btn-purple  no-border" type="button" name="btn-query" onclick="query_any()">
									<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
										查询
							</button>
							&nbsp;&nbsp;&nbsp;
							<button class="btn btn-grey  no-border" type="reset" onclick="Javascript:doAddPro();">
									<i class="ace-icon fa fa-undo icon-on-right bigger-110"></i>
										新增
							</button>
						</div>
					</div>						  
				</div><!-- /.row -->
				<hr style="display:none;" id="delete_hr">
					<div class="row">
						<div class="col-xs-12">
							<table id="jquery-table" class="table table-striped table-bordered table-hover">
							</table>
					</div><!-- /.col -->
				</div><!-- /.row -->
			</div><!-- /.page-content-area -->
		</form>
	    </div><!-- /.page-content -->
				
	  <%@ include file="/common/common-js.jsp" %>
	  <script type="text/javascript" src="${ctx}/js/call_func.js"></script>
	  <script src="${ctx}/ace/js/justTools.js"></script>
	  <script type="text/javascript">
		//【查询】按钮Click事件
		 function query_any(){
			$("#output").removeAttr("style");
			$(".load_bg").removeAttr("style");
			$(".load_img").removeAttr("style");
			if(jqTable != null){jqTable.fnDraw();}
			else{loaddata();}
			 $("#delete_btn").attr("style","display:block");
			 $("#delete_hr").attr("style","margin-top:0px;margin-bottom:20px;display:block");
		  }
 
		  var jqTable =null;
		  var loaddata = function(){
			jQuery(function($) {
					jqTable = $('#jquery-table').dataTable( {
	                    //数据列控制（包括对应返回JSON属性名称，对应表头列索引位置、数据转换封装等）
					    "columnDefs": [
					    	{	  
					    		  "title":"活动id",
					    	  	  "targets": [0],
					    	  	  "data": "id",
					    	  	  "name": "id",   //JSON返回属性
					    	  	  "bSortable": false,
					    	},
					    	{
					    		  "title":"活动名称",
					    		  "name":"activity_name",
					    		  "data": "activity_name",
					    	  	  "targets": [1], 
					    	  	  "bSortable": false,
								  "render": function(value, type, row) { // 返回自定义内容
									  return '<textarea class="form-control backup-name" row="3" data-id='+row.id+' style="height:100% !important;">'+value+'</textarea>';						 
								  },
					    	},
					    	{
					    		  "title":"时间",
					    		  "name":"ctime",
					    		  "data": "ctime",
					    	  	  "targets": [2], 
					    	  	  "bSortable": false,
					    	},
					    	{
					    		  "title":"备注",
					    		  "name":"remark",
					    		  "data": "remark",
					    	  	  "targets": [3], 
					    	  	  "bSortable": false,
								  "render": function(value, type, row) { // 返回自定义内容
									  return '<textarea class="form-control backup-text" row="3" data-id='+row.id+' style="height:100% !important;">'+value+'</textarea>';						 
								  },
					    	},
					    	{
					    	 	  "title":"操作",
					    	 	  "name":"id",
							 	  "data": "id",
					    	  	  "targets": [4] ,
//			 		    	  	  "width": "120px",
					    	  	  "bSortable": false,
								   "render": function(value, type, row) { // 返回自定义内容
									   var text="";
								   		text="<a class='btn btn-success btn-sm' href='javascript:void(0)' onclick='opstatus(" + row.id + ",\"" + row.demo +"\")'>编辑活动</a>";
								   		text+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn-success btn-sm' href='javascript:void(0)' onclick='edit(" + row.id + ",\"" + row.api_module_id +"\")'>编辑内容</a>";
									    text+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn btn-sm' href='javascript:void(0)' onclick='copyModel(" + row.id + ",\"" + row.demo +"\")'>复制活动</a>";
									    text+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn btn-sm' href='javascript:void(0)' onclick='copytotest(" + row.id + ")'>复制到测试</a>"
								   		return text;
						 		 	},
					    	},
	                  	],
						"sAjaxSource": ctx + cmdList + "?rand=" + Math.random(),//数据源URL
						"fnServerData": function ( sSource, aoData, fnCallback ) {//从服务器请求数据
					   			var data = $('#queryForm').serializeObject();
								data.aoData = JSON.stringify(aoData);
								$.ajax({
									"dataType": 'json',
									"type": "POST",
									"url": sSource,
									"data":data,
									"success": function(resp){
										fnCallback(resp);
										initListPage();
										$(".load_bg").attr("style","display:none");
										$(".load_img").attr("style","display:none");
									},
									"error": function(XMLHttpRequest, textStatus, errorThrown) {
										$(".load_bg").attr("style","display:none");
										$(".load_img").attr("style","display:none");
										alert('请求服务器错误,请刷新重试！！');//错误提示，包括服务器重启导致的session失效
									},
								});
						},
						"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
							//console.log("---" + nRow + "+++++" + aData["id"]);
							//=====================这里实现行双击事件======================//
							nRow.ondblclick = function() {
							}; 
						},
						"sScrollXInner": "100%",
						"sScrollY": "100%",//如果不设置，IE会有问题
						 "aaSorting": [[0,"desc"]] //初始化加载，默认按第几列排序、排序方式（列从0开始计算）
				    });
	
			});//end jquery初始化
		};
			
		
		function opstatus(id,memo){
		   var url = "/apiactivity/addPage.action?id="+id;
		   url = encodeURI(url); 
		   openDialogWithRand(url,1400,1000,"编辑活动",function (data){})
		}
		
		function edit(id,api_module_id){
			var url = ctx+"/module/activitymodule/apiModuleContent.jsp?avtivityId="+id+"&modelId="+api_module_id;
			window.location.href = url;
		}
		
		function copyModel(id,memo){
			var url=ctx + "/newcps/copyModel.action";
				 $.ajax({
					"url":url ,
					"type": "POST",
					"dataType": 'json',
					"data":  {"id":id},
					"beforeSend": function(){
						
					},
					"success": function(data){
						alert("操作成功");
						query_any();
					},
					"error": function(){
						alert("操作失败");								 
					},
					"complete": function(){
						//解除事件
						
					}
		       });
		}
		
		function updateModel(id,operation){
			var url=ctx + "/module/delModel.action";
			 $.ajax({
				"url":url ,
				"type": "POST",
				"dataType": 'json',
				"data":  {"id":id,"operation":operation},
				"beforeSend": function(){
					
				},
				"success": function(data){
					alert("操作成功");
					query_any();
				},
				"error": function(){
					alert("操作失败");								 
				},
				"complete": function(){
					//解除事件
					
				}
			}); 
		}
		
		function copytotest(id){
			var url=ctx + "/activitymodule/copytotest.action";
			 $.ajax({
				"url":url ,
				"type": "POST",
				"dataType": 'json',
				"data":  {"id":id},
				"beforeSend": function(){
					
				},
				"success": function(data){
					alert("操作成功");
					query_any();
				},
				"error": function(){
					alert("操作失败");								 
				},
				"complete": function(){
					//解除事件
					
				}
			}); 
		}
		
		function doAddPro() {	
			 var url = "/apiactivity/addPage.action";
			url = encodeURI(url); 
			openDialogWithRand(url,1000,800,"新增活动",function (data){});
		}
		
		jQuery(function(){
			query_any();
			$('#jquery-table').delegate('textarea.backup-text','blur',function(){
				var _this = $(this);
				var id = _this.attr('data-id');
				var val = _this.val();
				var data = "id=" + id + "&remark=" + val;
				$.ajax({
					"type": "POST",
					"dataType": 'json',
					"url": ctx + '/apiactivity/updateContext.action',
					"data": data,
					"timeout":300000,
					"success": function(data){
						backupTextJustToolsTip(_this,'备注成功','tooltip-suc');
					},
					"error": function(){
						backupTextJustToolsTip(_this,'备注失败','tooltip-fail');
					}
				});			
				return false;
			});
			
			$('#jquery-table').delegate('textarea.backup-name','blur',function(){
				var _this = $(this);
				var id = _this.attr('data-id');
				var val = _this.val();
				var data = "id=" + id + "&activityName=" + val;
				$.ajax({
					"type": "POST",
					"dataType": 'json',
					"url": ctx + '/apiactivity/updateContext.action',
					"data": data,
					"timeout":300000,
					"success": function(data){
						backupTextJustToolsTip(_this,'修改成功','tooltip-suc');
					},
					"error": function(){
						backupTextJustToolsTip(_this,'修改失败','tooltip-fail');
					}
				});			
				return false;
			});

		});
		
		function backupTextJustToolsTip(dom,content,theme){
			dom.justToolsTip({
				animation:"moveInLeft",
				contents:content,
				theme:theme,
				gravity:'left'
			});
			setTimeout(function(){
				$('.just-tooltip').remove();
			},3000); 
		}
	</script>
</body>
</html>
